package com.rightsidetech.http.controller.storeApp.login;

import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.request.AliPayRegReq;
import com.rightsidetech.system.dtos.request.SendValidCodeReq;
import com.rightsidetech.system.dtos.request.store.StoreAppBackReq;
import com.rightsidetech.system.dtos.request.store.StoreAppCodeLoginReq;
import com.rightsidetech.system.dtos.request.store.StoreAppLoginReq;
import com.rightsidetech.system.dtos.request.store.StoreAppRegistReq;
import com.rightsidetech.system.dtos.response.store.AppLoginInfoResp;
import com.rightsidetech.system.dtos.response.store.StoreAppLoginResp;
import com.rightsidetech.system.service.SendMessageService;
import com.rightsidetech.system.service.StoreLoginInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;

/**
 * @Package: com.rightsidetech.http.controller.storeApp.login
 * @ClassName: StoreLoginController
 * @Author: yinj
 * @CreateTime: 2021/11/04 13:52
 * @Description: 门店app登录相关
 */
@Slf4j
@RequestMapping("/storeApp/store")
@Api(tags = "门店app登录相关api")
@RestController
@ShenyuSpringMvcClient(path = "/storeApp/store/**")
public class StoreLoginController extends BaseController {

    @Resource
    private StoreLoginInfoService storeLoginInfoService;
    @Resource
    private SendMessageService sendMessageService;

    @Log
    @PostMapping("/login")
    @ApiOperation("门店登录")
    public ResponseData<StoreAppLoginResp> login(@RequestBody StoreAppLoginReq req){
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        req.setClientIp(getAttributeForIp());
        return storeLoginInfoService.login(req);
    }

    @Log
    @ApiOperation("验证码登录")
    @PostMapping("/code_login")
    public ResponseData<StoreAppLoginResp> codeLogin(@RequestBody StoreAppCodeLoginReq req){
        ConstraintViolation validate = BeanValidator.validate(req);
            if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        // TODO: 2021-11-5 10:11:19 测试用验证码，后面需删除
        if (!"666666".equals(req.getVerifyCode())){
            //校验验证码
            AliPayRegReq aliPayRegReq = new AliPayRegReq();
            aliPayRegReq.setPhone(req.getPhoneNo());
            aliPayRegReq.setVerifyCode(req.getVerifyCode());
            ResponseData data = sendMessageService.checkCerifyCode(aliPayRegReq);
            if (data.getCodeId() != CodeIdEnum.Success.getValue()) {
                return data;
            }
        }
        req.setClientIp(getAttributeForIp());
        return storeLoginInfoService.codeLogin(req);
    }

    @Log
    @ApiOperation("找回密码")
    @PostMapping("/back")
    public ResponseData back(@RequestBody StoreAppBackReq req){
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        // TODO: 2021-11-9 10:22:07 测试用验证码，后面需删除
        if (!"666666".equals(req.getVerifyCode())){
            //校验验证码
            AliPayRegReq aliPayRegReq = new AliPayRegReq();
            aliPayRegReq.setPhone(req.getPhoneNo());
            aliPayRegReq.setVerifyCode(req.getVerifyCode());
            ResponseData data = sendMessageService.checkCerifyCode(aliPayRegReq);
            if (data.getCodeId() != CodeIdEnum.Success.getValue()) {
                return data;
            }
        }
        return storeLoginInfoService.back(req);
    }

    @Log
    @ApiOperation(value = "发送验证码")
    @RequestMapping(value = "/sendCode", method = RequestMethod.GET)
    public ResponseData sendValidCode(SendValidCodeReq codeReq) {
        try {
            return sendMessageService.sendValidCode(codeReq);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.fail(CodeIdEnum.ServerError);
        }
    }

    @Log
    @Token
    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public ResponseData logout(){
        String loginId = getAttributeForLoginId();
        if (StringUtils.isEmpty(loginId)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        return storeLoginInfoService.logout(Long.parseLong(loginId));
    }


    @Log
    @Token
    @GetMapping
    @ApiOperation("登录获取门店信息")
    public ResponseData<AppLoginInfoResp> getLoginInfo(){
        Long storeInfoId = Long.parseLong(getAttribute());
        return storeLoginInfoService.getLoginInfo(storeInfoId);
    }

    @ApiIgnore
    @Log
    @PostMapping("/regist")
    @ApiOperation("注册(ios功能)")
    public ResponseData regist(@RequestBody StoreAppRegistReq req){
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        // TODO: 2021-11-5 10:11:19 测试用验证码，后面需删除
        if (!"666666".equals(req.getVerifyCode())){
            //校验验证码
            AliPayRegReq aliPayRegReq = new AliPayRegReq();
            aliPayRegReq.setPhone(req.getPhoneNo());
            aliPayRegReq.setVerifyCode(req.getVerifyCode());
            ResponseData data = sendMessageService.checkCerifyCode(aliPayRegReq);
            if (data.getCodeId() != CodeIdEnum.Success.getValue()) {
                return data;
            }
        }
        return storeLoginInfoService.regist(req);
    }


}
